package com.jurassic.demo.service;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

/**
 * @Author pan_jin
 * @Description: $
 * @Date: 2022/7/15 9:45
 * @Version: 1.0.0
 * @Slogan: 梦想还是要有的万一实现了呢
 */
@Configuration
@PropertySource("jdbc.properties")
public class JdbcConfiguration {

    @Value("${jdbc.url}")
    private String url;

    @Value("${jdbc.user}")
    private String user;

    @Value("${jdbc.password}")
    private String password;

    @Bean
    DataSource getDataSource() {
        HikariConfig config = new HikariConfig();
        config.setIdleTimeout(300);
        config.setConnectionTimeout(500);
        config.setPassword(password);
        config.setJdbcUrl(url);
        config.setUsername(user);
        return new HikariDataSource(config);
    }

    @Bean
    public JdbcTemplate createJdbcTemplate(@Autowired DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }


}
